export function getImge(file) {
  let arr = ["png", "jpg", "jpeg", "bmp", "gif", "webp", "psd", "svg", "tiff"];
  let ext = "";
  let ext2 = "";
  if (file.name) {
    ext = file.name.split(".").pop();
  }
  if (file.response) {
    ext2 = file.response.map.fileName.split(".").pop();
  }
  if (arr.indexOf(ext.toLowerCase()) == -1) {
    let suffix = file.url.split(".").pop();
    file.name = file.name + "." + suffix;
  }
  if (arr.indexOf(ext2.toLowerCase()) == -1) {
    let suffix = file.response.map.filePath.split(".").pop();
    file.response.map.fileName = file.response.map.fileName + "." + suffix;
  }
  return file;
}
export async function fileDown1(file, name, val, peopleval) {
  console.log(file);
  let isload = "";
  let fileName = "";
  let fileUrl = "";
  let methodval = "get";
  if (val == 1) {
    fileName = name;
    fileUrl = file;
    methodval = "post";
  } else {
    fileName = file.name;
    fileUrl = file.response.map.filePath;
  }
  if (peopleval == 1) {
    methodval = "get";
  }
  await this.Sevice({
    url: fileUrl,
    method: methodval,
    headers: { "Content-Type": "multipart/form-data" },
    responseType: "blob",
  })
    .then((response) => {
      let blob = new Blob([response.data], {
        type: "application/vnd.ms-excel",
      });
      let objectUrl = URL.createObjectURL(blob); // 创建URL
      const link = document.createElement("a");
      link.href = objectUrl;
      // link.download = '.xlsx'// 自定义文件名
      //这里是获取后台传过来的文件名
      link.setAttribute("download", fileName);
      link.click(); // 下载文件
      URL.revokeObjectURL(objectUrl); // 释放内存
      isload = objectUrl;
    })
    .catch((err) => {
      isload = "";
      console.log(err);
    });
  return isload;
}
export async function fileDown(file) {
  console.log(file);
  let isload = "";
  let fileName = "";
  let fileUrl = "";
  let methodval = "get";
  if (file.response) {
    fileName = file.name;
    fileUrl = file.response.map.filePath;
  } else {
    fileName = file.name;
    fileUrl = file.url;
  }
  await this.Sevice({
    url: fileUrl,
    method: methodval,
    headers: { "Content-Type": "multipart/form-data" },
    responseType: "blob",
  })
    .then((response) => {
      let blob = new Blob([response.data], {
        type: "application/vnd.ms-excel",
      });
      let objectUrl = URL.createObjectURL(blob); // 创建URL
      const link = document.createElement("a");
      link.href = objectUrl;
      // link.download = '.xlsx'// 自定义文件名
      //这里是获取后台传过来的文件名
      link.setAttribute("download", fileName);
      link.click(); // 下载文件
      URL.revokeObjectURL(objectUrl); // 释放内存
      isload = objectUrl;
    })
    .catch((err) => {
      isload = "";
      console.log(err);
    });
  return isload;
}
export async function fileDown2(file) {
  console.log(file);
  let isload = "";
  let fileName = "";
  let fileUrl = "";
  let methodval = "get";
  if (file.response) {
    fileName = file.fileName;
    fileUrl = file.response.map.filePath;
  } else {
    fileName = file.fileName;
    fileUrl = file.filePath;
  }
  await this.Sevice({
    url: fileUrl,
    method: methodval,
    headers: { "Content-Type": "multipart/form-data" },
    responseType: "blob",
  })
    .then((response) => {
      let blob = new Blob([response.data], {
        type: "application/vnd.ms-excel",
      });
      let objectUrl = URL.createObjectURL(blob); // 创建URL
      const link = document.createElement("a");
      link.href = objectUrl;
      // link.download = '.xlsx'// 自定义文件名
      //这里是获取后台传过来的文件名
      link.setAttribute("download", fileName);
      link.click(); // 下载文件
      URL.revokeObjectURL(objectUrl); // 释放内存
      isload = objectUrl;
    })
    .catch((err) => {
      isload = "";
      console.log(err);
    });
  return isload;
}
export async function fileLook(file) {
  let objectUrl = "";
  await this.Sevice({
    url: file,
    method: "get",
    headers: { "Content-Type": "multipart/form-data" },
    responseType: "blob",
  })
    .then((response) => {
      let blob = new Blob([response.data], {
        type: "application/vnd.ms-excel",
      });
      objectUrl = URL.createObjectURL(blob); // 创建URL
    })
    .catch((err) => {
      console.log(err);
    });
  return objectUrl;
}
export async function filePdf(file) {
  let objectUrl = "";
  await this.Sevice({
    url: file,
    method: "get",
    headers: { "Content-Type": "multipart/form-data" },
    responseType: "blob",
  })
    .then((response) => {
      let blob = new Blob([response.data], { type: "application/pdf" });
      objectUrl = URL.createObjectURL(blob); // 创建URL
      console.log(objectUrl);
    })
    .catch((err) => {
      console.log(err);
    });
  return objectUrl;
}
